import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.util.*;
//Current Status - The manufacturer method is working. 
//TO DO - Make the other methods return an arraylist for the drop down menu to keep updating.
//TO DO - Make the vendor to be working to.
// TO DO - Insertion,deletion, Update - Add a text area at LAyout.SOUTH

public class QueryGui extends JFrame 
{
	ArrayList<String> values = new ArrayList<String>();// The selected values.

	private String selected = "";

	private JTextField displayField = new JTextField(30);

	JFrame frame = new JFrame();

	private JComboBox c = new JComboBox();

	private JButton b = new JButton("Add item");


	private int count = 0, p = 0;;
	String manuf ="" ;
	String model = "", year= "", description = "",litres, inches= "", engine= "";

	ArrayList<String> temp;
	//ArrayList<String> temp2;


	public QueryGui(Connection connection, ConnectDb connectDb) 
	{

		final Query q = new Query(connection, connectDb);
		final JPanel displayPanel = new JPanel() ; // the upper panel.
		final JPanel optionPanel = new JPanel() ; // the 2nd panel which displays the content.
		temp  = q.queryManufac();
		int x = temp.size();
		p++;
		for(int i = 0;i<x;i++)
			c.addItem(temp.get(i));
		displayField.setEditable(false);
		b.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) 
			{			
				selected = (String) c.getSelectedItem();
				System.out.println(selected);
				if(selected == null)
				{
					selected = "";
				}
				if(count == 0)
					temp = q.queryModel(selected.trim());
				else if(count == 1)
					temp = q.queryYear(selected.trim());
				else if(count == 2)
				{
					temp = q.queryLitres(selected.trim());
					printList(temp);
				}
				else if(count == 3)
				{
					System.out.println("selected:" + selected);
					temp = q.queryEngineType(selected.trim());
					printList(temp);
				}
				else if(count == 4)
					temp = q.queryCubicInches(selected.trim());
				else if(count == 5)
					temp = q.queryDesc(selected.trim());
				else if(count == 6)
					temp = q.queryRlink(selected.trim());
				else if(count == 7)
				{
					displayPanel.setVisible(false);
					optionPanel.setVisible(false);
					JPanel panel = new JPanel();

					ArrayList<String> parts = q.queryPNum(temp.get(0));
					printList(parts);

					JList jlist = new JList(parts.toArray());
					JScrollPane pane = new JScrollPane(jlist);

					panel.add(pane);
					panel.setVisible(true);
					frame.add(panel);
					frame.repaint();
				}

				c.removeAllItems();

				for(int i = 0; i < temp.size(); i++)
				{
					c.addItem(temp.get(i));
				}

				count++;
			}
		});
		
		optionPanel.setLayout(new FlowLayout());
		displayPanel.add(displayField);
		optionPanel.add(c);
		optionPanel.add(b);
		//connectDb.disconnectFromDB();

		frame.setLayout(new BorderLayout());
		frame.add(displayPanel,BorderLayout.NORTH);
		frame.add(optionPanel,BorderLayout.CENTER);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(400, 400);
		frame.setVisible(true);

	}

	public void printList(ArrayList<String> a)
	{
		for(int i = 0; i < a.size(); i++)
			System.out.println("LIST:" + a.get(i));
	}



}

